# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from django.utils.html import format_html
from .models import Location,WxApp,Splashes

class WxAppAdmin(admin.ModelAdmin):
    model = WxApp
    fieldsets = (
        ['Main', {
            'fields': ('app_name', 'app_id','splash_random', 'splash_default'),
        }],
        ['Secrets',{
            'classes': ('collapse',),
            'fields': ('app_secret',"secret_token", "encoding_aes_key"),
        }])
    list_display = ["id", "app_name", "app_id", "splash_random", "splash_default"]
    readonly_fields = ['create_time','update_time']
    date_hierarchy = 'update_time'
    ordering = ["-update_time"]

class LocationAdmin(admin.ModelAdmin):
    model = Location
    list_display = ["id", "latitude", "longitude", "precision", "create_time", "update_time"]
    readonly_fields = ['create_time','update_time']
    date_hierarchy = 'update_time'
    ordering = ["-update_time"]

class SplashesAdmin(admin.ModelAdmin):
    def image_file_btn(obj):
        position = str(obj.content).find("static")
        return format_html('<a href="/static/{0}">查看</a>' .format(str(obj.content)[position+7:]))

    image_file_btn.short_description = '更新文件'

    model = Splashes
    radio_fields = {'status': admin.HORIZONTAL}
    list_display = ["name", image_file_btn, "status","update_time"]
    list_filter = ['status']
    readonly_fields = ['update_time']
    date_hierarchy = 'update_time'
    ordering = ["-update_time"]

admin.site.register(WxApp, WxAppAdmin)
admin.site.register(Splashes, SplashesAdmin)
admin.site.register(Location, LocationAdmin)